import requests
import re
import csv

url = "https://movie.douban.com/top250"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36"

}
resp = requests.get(url, headers=headers)
page_content = resp.text

# 解析数据
obj = re.compile(r'<li>.*?<div class="item">.*?<span class="title">(?P<name>.*?)'
                 r'</span>.*?<p class="">.*?<br>(?P<year>.*?)&nbsp'
                 r'.*?<span class="rating_num" property="v:average">(?P<score>.*?)</span>'
                 r'.*?<span>(?P<peoplesay>.*?)人评价</span>', re.S)
# 开始匹配
result = obj.finditer(page_content)
f = open("爬到的数据/data.csv", mode="w", encoding="utf-8")
csvwriter = csv.writer(f)
for i in result:
    # print(i.group("name"))
    # print(i.group("year").strip())
    # print(i.group("score"))
    # print(i.group("peoplesay"))
    dic = i.groupdict()
    dic['year'] = dic['year'].strip()
    dic['score'] = dic['score'] + "分"
    dic['peoplesay'] = dic['peoplesay'] + "份评价"
    csvwriter.writerow(dic.values())

f.close()
print("搞定！")
